package com.wl.week_01.Homework_01;

/**
 * 纸质书类，继承自Book类并实现Rentable接口
 * @author TaiZai
 * @data 2024/9/20 17:35
 */
public class PrintedBook extends Book implements Rentable {
    private Integer pageCount;
    private Boolean isBorrowed = false;

    @Override
    public void rent() {
        // 借阅图书
        if (this.getBorrowed()) {
            throw new IllegalStateException("此本书已被借出。");
        } else {
            this.setBorrowed(true);
            System.out.println("图书 《" + super.getTitle() + "》 已成功借出");
        }
    }

    @Override
    public void returnBook() {
        // 归还图书
        if (!this.isBorrowed) {
            // 抛出 IllegalStateException。
            throw new IllegalStateException("此本书暂未被借出，无法归还");
        } else {
            this.setBorrowed(false);
            System.out.println("图书 《" + super.getTitle() + "》 已成功归还");
        }
    }

    public String getDetails() {
        return String.format("%-5s\t%-5s\t %-5s\t%-5.2f\t\t%-5s\t%-5s\t%-5s",
                getTitle(), "纸质书", getAuthor(), getPrice(), pageCount, "-",
                (this.getBorrowed() ? "已借出" : "未借出"));
    }

    public PrintedBook(String title, String author, Double price, Integer pageCount) {
        super(title, author, price);
        this.pageCount = pageCount;
    }

    public Integer getPageCount() {
        return pageCount;
    }

    public void setPageCount(Integer pageCount) {
        this.pageCount = pageCount;
    }

    public Boolean getBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(Boolean borrowed) {
        isBorrowed = borrowed;
    }
}
